/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package extensions.romario.server;

import extensions.NetworkClient;

/**
 * 
 * @author Manu
 */
public class Lobby {

	private static final int MAX_SLOTS = 4;
	private int _slotsCount = 0;
	protected NetworkClient[] _slots = new NetworkClient[MAX_SLOTS];

	public int getPlayersCount() {
		return _slotsCount;
	}

	public boolean hasFreeSlots() {
		return _slotsCount < 4;
	}

	public int getSlotPos(NetworkClient client) {
		for (int i = 0; i < MAX_SLOTS; i++) {
			if (_slots[i] == client) {
				return i;
			}
		}

		throw new IllegalArgumentException("Client '" + client
				+ "' is not in lobby.");
	}

	public void reserveSlot(NetworkClient client) {
		if (!hasFreeSlots()) {
			throw new IllegalStateException("Lobby is full");
		}

		for (int i = 0; i < _slots.length; i++) {
			if (_slots[i] == null) {
				_slots[i] = client;
				_slotsCount++;
				break;
			}
		}
		System.out.println("Joined:" + client);
	}

	public void printLobbyInfo() {
		System.out.println("Lobby info:");

		for (int i = 0; i < _slots.length; i++) {
			System.out.println("  Slot " + i + ": " + _slots[i]);
		}
	}

	public int kickSlot(NetworkClient client) {
		System.out.println("Kicking:" + client);
		for (int i = 0; i < MAX_SLOTS; i++) {
			if (_slots[i] == client) {
				_slots[i] = null;
				_slotsCount--;
				return i;
			}
		}

		throw new IllegalArgumentException("Client '" + client
				+ "' is not in the lobby.");
	}
}
